<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function arr(boxTypes, truckSize) {
      boxTypes.sort((a, b) => b[1] - a[1])
      let count = 0
      for (let i = 0, len = boxTypes.length; i < len; i++) {
        if (truckSize <= 0) break
        count += Math.min(truckSize, boxTypes[i][0]) * boxTypes[i][1]
        truckSize -= boxTypes[i][0]
      }
      return count
    };
    console.log(arr([[1, 3], [2, 2], [3, 1]], 4));
  </script>
</body>

</html>